<?php

class Salesman_manage  extends AdminManage
{
	private $_pagesize=10;//分页个数
	/**
	 * 初始化
	 */
	public function _init()
	{

	}

	/**
	 * 默认动作
	 */
	protected function _default()
	{
		#审核类型
		$type= $this->getParam('type');
		switch($type) {
			case 'pass':
				$nodatainfo =  '没有相匹配状态的业务人员信息';
				break;
			case 'no_pass':
				$nodatainfo = '没有相匹配状态的业务人员信息';
				break;
			default:
				$nodatainfo = '没有找到相匹配的业务人员信息';
		}

		#获取参数
		//关键字
		$key = $this->getParam('kw');
//		if($key != null){
//			$key = Encode::decodeURI($key);
//		}


		//时间
		$start=$this->getParam('start');
		$end=$this->getParam('end');


		#组合查询语句
		$strWhere = '';

		if(empty($type)){
			$strCheckState = '  checkState in(0,1)';
		}else{
			#审核状态
			switch($type) {
				case 'pass':
					$checkState = 1;
					break;
				case 'no_pass':
					$checkState = 0;
					break;

			}

			$strCheckState = '  checkState = '.intval($checkState);

		}

		if(!empty($key)){
			$strWhereKey = ' and   CONCAT(name,mobile) like  \'%'.$key.'%\''  ;
			//				$strWhereKey= "and title like '%$key%'";
		}

		if($key==0&&is_numeric($key)){
			$strWhereKey = ' and   CONCAT(name,mobile) like  \'%0%\' ';
		}

		if($start && $end){
			$start1=$start/1000;
			$end1=$end/1000+86400;
			$strWhereStart=' and dateline between  '.$start1. ' and ' .$end1  ;
		}


		#查询条件
		$strWhere = $strWhere.$strCheckState.$strWhereKey.$strWhereStart;

		#获取数据总数
		$total = DB::getRows('salesman',$strWhere);


		/**分页操作 start**/
		$p = 1; //当前页码
		if (isset($_GET['p']) && is_numeric($_GET['p'])) {
			$p =intval($_GET['p']);
			if ($p<=1) {
				$p = 1;
			}
		}

		$totalPage = ceil($total/$this->_pagesize);//计算总页数
		$begin = ($p-1)*$this->_pagesize;//计算开始游标
		$limit = "$begin,$this->_pagesize";//计算分页



		$pageObj = new Dpage($total, $this->_pagesize, $p);
		$pageCss = $pageObj->pageCss("1");
		$pageform = $pageObj->outputPages(1,"<li>","</li>");

		/**分页操作 end**/

//DB::echoExeSql();
		#获取数据信息
		$list = new publicGetDataModel();
		$listData = $list->getData($where = $strWhere,$limit = $limit,$fields = 'id,name,sex,mobile,tel,email,checkState,dateline',$order = 'dateline',$table='salesman');

		if(empty($listData)) {
			if (isset($key)) {
				$nodatainfo = '没有找到相匹配的业务人员信息';
			}
		}



		#模板赋值
		$now=time();
		$this->assign('now',$now);
		$this->assign('classify_list',$listData);
		$this->assign('pageCss',$pageCss);//分页样式
		$this->assign('pageform',$pageform);//分页标签
		$this->assign('type',$type);
		$this->assign('kw',$key);
		$this->assign('start',$start/1000);
		$this->assign('end',$end/1000);
		$this->assign('nodatainfo',$nodatainfo);


		$this->assign('menu_navbar','业务人员管理  -- <a href="'.$_GET['action'].'.html">业务人员列表</a>');
		$this->tpl_full_path('/admins/salesman_manage/index');
	}
	
	
	
	/**
	**添加业务员
	**/
	public function _add()
	{
		$id = $this->getParam('id','i');

		if($_SERVER["REQUEST_METHOD"] === "POST"  && $_POST['sub']==1 ){
			$data['name'] = $this->getParam('name','t');#
			$data['mobile'] = $this->getParam('mobile','t');#
			$data['email'] = $this->getParam('email','t');#
			$data['dateline']  = time();
			$data['checkState'] = 1;

			if($id>0){ //编辑

				$data['checkState'] = 1;
				if(DB::update("salesman",'id='.$id,$data)){
					$this->alertBack("保存成功！");
				}else{
					$this->alertBack("编辑失败,请重试！");
				}
			}else{
				if(DB::insert("salesman",$data)){
					$this->alertBack("保存成功！");
				}else{
					$this->alertBack("保存失败,请重试！");
				}

			}

		}//end 编辑发布提交表单


		if($id) {  //编辑
			$res = DB::select('select * from salesman where id ='.$id );



			$this->assign('data',$res[0]);
		}



        $this->assign('menu_navbar','业务人员管理  -- <a href="'.$_GET['action'].'.html">新增业务员</a>');
		$this->tpl_full_path('/admins/salesman_manage/add_form');
	}

	/**
	 * 注销
	 */
	protected function _cancal(){

		$id = $this->getParam('id','t');
		if(isset($id) && !empty($id) ){
			if(strpos($id,',')){
				$where = "id in (".$id.")";
			}else{
				$where = "id = {$id}";
			}
			$result = DB::update('salesman',$where,array('checkState'=>'0'));

			if($result){
				die('1');
			} else {
				die('0');
			}
		}else{ //参数错误
			die('2');
		}
	}


}
?>
